package com.vogue.supplymall.payment.dto;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import java.util.Map;

public abstract class BaseDTO {

    @Override
    public String toString() {
        StringBuffer strbuf = new StringBuffer("");
        List<Field> allFields = new ArrayList<Field>();
        allFields.addAll(Arrays.asList(this.getClass().getDeclaredFields()));
        Class<?> supercalss = this.getClass().getSuperclass();
        while (supercalss != null && supercalss != BaseDTO.class ) {
            allFields.addAll(Arrays.asList(supercalss.getDeclaredFields()));
            supercalss = supercalss.getSuperclass();
        }
        for (Field field : allFields) {
            try {
                field.setAccessible(true);
                if (Modifier.isFinal(field.getModifiers())) {
                    continue;
                } else {                    
                    if (field.getType() == List.class) {
                        strbuf.append(",\"").append(field.getName()).append("\":");
                        if (field.get(this) == null) {
                            strbuf.append("null");
                        } else {
                            StringBuffer listStrBuf = new StringBuffer();
                            for (Object obj : (List<?>)field.get(this)) {
                                listStrBuf.append(",").append(obj.toString());
                            }
                            strbuf.append("[");
                            if (listStrBuf.length() > 0) {
                                strbuf.append(listStrBuf.substring(1));
                            }
                            strbuf.append("]");
                        }
                    } else if (field.getType() == Map.class) {
                        strbuf.append(",\"").append(field.getName()).append("\":");
                        if (field.get(this) == null) {
                            strbuf.append("null");
                        } else {
                            StringBuffer mapStrBuf = new StringBuffer("");
                            Map<String, Object> map = (Map<String, Object>)field.get(this);
                            for (String key : map.keySet()) {
                                mapStrBuf.append(",\"").append(key).append("\":");
                                if (map.get(key) instanceof String) {
                                    mapStrBuf.append("\"").append(map.get(key).toString()).append("\"");
                                } else {
                                    mapStrBuf.append(map.get(key).toString());
                                }                                
                            }
                            strbuf.append("{");
                            if (mapStrBuf.length() > 0) {
                                strbuf.append(mapStrBuf.substring(1));
                            }
                            strbuf.append("}");
                        }
                    } else {
                        strbuf.append(",\"").append(field.getName()).append("\":");
                        if (field.get(this) == null) {
                            strbuf.append("null");
                        } else {
                            strbuf.append("\"").append(field.get(this).toString()).append("\"");
                        }
                    }                    
                }
            } catch (IllegalArgumentException | IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if (strbuf.length() > 0) {
            return "{" + strbuf.substring(1) + "}"; 
        } else {
            return "{}";
        }        
    }
}
